/**
 *  EKology - An Ecosystem Simulation Framework
 *  Copyright (C) 2008  Aarón Tavío - aaron.tavio at gmail.com - ULPGC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package fungusEater.datatypes;

import net.ekology.ekosystem.datatypes.Genome;

import java.util.Hashtable;
import java.io.Serializable;


/**
 * @author Aarón Tavío - aaron.tavio at gmail.com
 * @version 1.0.0 - 20081019-1700
 */
public class CommunityStats implements Serializable {
    // Attributes
    private Hashtable<String,Double> htOreCollected;
    private Hashtable<String,Genome> htCollectorGenome;
    
    
    
    // Methods
    public CommunityStats() {
        htOreCollected = new Hashtable<String,Double>();
        htCollectorGenome = new Hashtable<String,Genome>();
    }
    
    
    public void addOreCollected(String sCollectorID, double dAmount) {
        double dCurrentAmount;
        
        dCurrentAmount = htOreCollected.get(sCollectorID);
        htOreCollected.put(sCollectorID, dCurrentAmount+dAmount);
    }
    
    
    public void addCollector(String sCollectorID, Genome oGenome) {
        if (!contains(sCollectorID)) {
            htCollectorGenome.put(sCollectorID,oGenome);
            htOreCollected.put(sCollectorID,0.0);
        }
    }
    
    
    public Genome getBestGenome() {
        Genome[] aGenome;
        
        aGenome = getBestGenomes(1);
        
        return aGenome[0];
    }
    
    
    public Genome[] getBestGenomes(int iNumber) {
        Genome[] aGenome;
        double dMaxCollectedAmount;
        double dOreCollected;
        String sBestCollector;
        Hashtable<String,Double> htOre;
        
        
        if (iNumber > htOreCollected.size()) iNumber = htOreCollected.size();
        
        htOre = (Hashtable<String,Double>) htOreCollected.clone();
        aGenome = new Genome[iNumber];
        
        for(int i=0; i < iNumber; i++) {
            dMaxCollectedAmount = 0;
            sBestCollector = null;
            
            for(String sCollectorID: htOre.keySet()) {
                dOreCollected = htOre.get(sCollectorID);
                if (sBestCollector == null || dOreCollected > dMaxCollectedAmount) {
                    dMaxCollectedAmount = dOreCollected;
                    sBestCollector = sCollectorID;
                }
            }
            
            htOre.remove(sBestCollector);
            aGenome[i] = htCollectorGenome.get(sBestCollector);
        }
        
        
        return aGenome;
    }
    
    public int getCommunitySize() {
        return htCollectorGenome.size();
    }
    
    public boolean contains(String sCollectorID) {
        return htCollectorGenome.containsKey(sCollectorID);
    }
}



